package cn.tedu._05mvcboot02.controller;

import cn.tedu._05mvcboot02.mapper.UserMapper;
import cn.tedu._05mvcboot02.pojo.dto.UserInsertDTO;
import cn.tedu._05mvcboot02.pojo.entity.User;
import cn.tedu._05mvcboot02.pojo.vo.UserListVO;
import cn.tedu._05mvcboot02.pojo.vo.UserinfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
@Api(tags = "用户模块")
@RestController
@RequestMapping("/v1/users")
public class UserController {
    @Autowired
    private UserMapper userMapper;


    @ApiImplicitParam(name = "添加用户",value = "用户", required = true,dataType = "userInsertDTO")
    @PostMapping("/insert")
    public String insert (UserInsertDTO userInsertDTO){
        User user = new User();
        BeanUtils.copyProperties(userInsertDTO, user);
        user.setCreated(new Date());

        userMapper.insertUser(user);
        return "添加成功";
    }
    @ApiImplicitParam(value = "登录")
    @GetMapping("/userList")
    public List<UserListVO> userListVOS(){

        return userMapper.selectUserList();
    }



    @ApiImplicitParam(value = "删除")
    @GetMapping("/delete")
    public String delete(int id){
        int a = userMapper.deleteUserById(id);

         return "删除成功"+a;
    }


    @ApiImplicitParam(value = "更新")
    @PostMapping("/update")
    public String update(User user){
        userMapper.updateById(user);
        return "修改成功";
    }

    @ApiImplicitParam(value = "忘了是啥")
    @GetMapping("{id}")
    public UserinfoVO getUserinfo(@PathVariable int id){
        return userMapper.selectUserinfoById(id);
    }

}































